﻿#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#pragma execution_character_set("utf-8")

#include <QMainWindow>
#include <QTimer>
#include <QDebug>
#include "BusinessLayer/ServiceDemoVoltage.h"
#include "BusinessLayer/ServiceDemoFuelFiller.h"
#include "BusinessLayer/Containers.h"
#include <ui_mainwindow.h>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

    AUTO_WIRED_SERVICE(BusinessLayer::ServiceDemoVoltage, voltage);
    AUTO_WIRED_SERVICE(BusinessLayer::ServiceDemoFuelFiller, fuelFiller);
public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
private slots:
    void on_switch_on_clicked()
    {
        voltage->Switch(!voltage->IsOn());
    }

    void on_increase_clicked()
    {
        voltage->IncreaseVoltage();
    }

    void on_decrease_clicked()
    {
        voltage->DecreaseVoltage();
    }

    void on_timer()
    {
        QString str;
        str.sprintf("%3.1f", voltage->GetVoltage());
        ui->voltage->setText(str);

        if (voltage->GetWarning())
            ui->voltage->setStyleSheet("QLabel{color:rgb(255,0,0);}");
        else
            ui->voltage->setStyleSheet("QLabel{color:rgb(0,0,0);}");

        float v = GET_POOL_DATA("voltage.voltage").toFloat();
        str.sprintf("%3.1f", v);
        ui->voltageFromPool->setText(str);
        bool isWarning = GET_POOL_DATA("voltage.warning").toBool();
        if (isWarning)
            ui->voltageFromPool->setStyleSheet("QLabel{color:rgb(255,0,0);}");
        else
            ui->voltageFromPool->setStyleSheet("QLabel{color:rgb(0,0,0);}");

        str = GET_POOL_DATA("clock.datetime").toString();
        ui->dateTime->setText(str);
    }

    void on_error(int code, const QString& message)
    {
        if (code == 0)
            ui->voltageWarning->setVisible(false);
        else
        {
            ui->voltageWarning->setText(message);
            ui->voltageWarning->setVisible(true);
        }
    }

    void dataChanged(const QString& objFieldName, const QVariant& v)
    {
        Q_UNUSED(objFieldName)
        QString str;
        str.sprintf("%3.1f", v.toFloat());
        ui->voltageForCallback->setText(str);
    }

    void on_startFiller_clicked()
    {
        fuelFiller->Start();
    }

    void on_stopFiller_clicked()
    {
        fuelFiller->Stop();
    }

    void on_data_changed_fuel_filler(const QString& objFieldName, const QVariant& v)
    {
        if (objFieldName == "valve.valveStatus")
        {
            ui->valveStatus->setText(v.toBool() ? "阀门已打开" : "阀门已关闭");
        }
        else if (objFieldName == "pump.rpm")
        {
            QString str;
            str.sprintf("油泵转速: %d rpm", v.toInt());
            ui->pumpRPM->setText(str);
        }
        else if (objFieldName == "pump.working")
        {
            ui->pumpStatus->setText(v.toBool() ? "油泵启动" : "油泵停止");
        }
        else if (objFieldName == "fuelFiller.fillingProcess")
        {
            QString str;
            str.sprintf("加油完成量：%d%%", v.toInt());
            ui->percentage->setText(str);
        }
        else if (objFieldName == "fuelFiller.status")
        {
            switch (v.toInt())
            {
            case 0:
                ui->fillingStatus->setText("加油状态：待机中");
                break;
            case 1:
                ui->fillingStatus->setText("加油状态：启动");
                break;
            case 2:
                ui->fillingStatus->setText("加油状态：正在加油");
                break;
            case 3:
                ui->fillingStatus->setText("加油状态：停止");

            }
        }
    }
private:
    Ui::MainWindow *ui;
    QTimer timer;
};
#endif // MAINWINDOW_H
